var layer, form, laydate, table, autocomplete;
var _crt_file;
layui.use(['form', 'laydate', 'layer'], function () {
	layer = layui.layer;
	form = layui.form;
	laydate = layui.laydate;

	$('.close').click(function () {
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	});
	_crt_file = {
		events: {
			export: function (obj) {
				var id=$(obj).attr('id');
				window.open("/supplier/exportFile?id=" + id);
			},
			plDown:function(obj){
				var ids=[];
				$(".filelistdiv").find(".filecheck:checked").each(function (i,v) {
					var value=$(v).attr("value");
					ids.push(value);
				})
				if(ids.length==0){
					layer.msg("请先选择需要下载的文件", {icon: 2});
					return false;
				}
				 ids = ids.join(",");
				window.open("/supplier/dic/download?fileIds="+ids);
			},
			allDown:function(obj){
                 var proId= $(obj).attr("proId")
				window.open("/supplier/dic/alldownload?projectId="+proId);
			},
			viewFile: function (obj) {
				var previewId=$(obj).attr('id');
				var b=$(obj).attr('url');
				var fileExtension = b.substring(b.lastIndexOf('.') + 1).toLowerCase();
				var fileType=_crt_file.events.formatfile(fileExtension);
				if(fileType==1){
					var imgSrc = b;
					var w = ($(window).width() *0.8);
					var h = ($(window).height() *0.8);
					imgSrc=imgSrc+"?v="+Math.random();
					_crt_file.events.getImageWidth(imgSrc,function(imgW,imgH){
						if(imgW*1 > w*1){
							imgW = w*1;
						}
						if(imgH*1 > h*1){
							imgH = h*1;
						}
						layer.open({
							type: 1,
							title: false,
							closeBtn: 0,
							area: [imgW+"px",imgH+"px"],
							skin: 'layui-layer-nobg', //没有背景色
							shadeClose: true,
							content: '<img src="/supervisor/projectManager/img?fid=' + previewId + '" height="'+imgH+'px" width="'+imgW+'px" />'
						});
					})
				}else if(fileType==2){
					layer.open({
						type: 1,
						title: false,
						closeBtn: 0,
						area: ["80%","80%"],
						skin: 'layui-layer-nobg', //没有背景色
						shadeClose: true,
						content: '<video src="'+b+'" controls="controls" style="width: 100%;height: 100%"></video>'
					});
				}else{
					POBrowser.openWindowModeless('/sys/office/openwordTemp?ft=1&fid=' + previewId, 'width=1200px;height=800px;', '{}')
				}
			},
			getImageWidth:function (url, callback) {
				var img = new Image();
				img.src = url;
				// 如果图片被缓存，则直接返回缓存数据
				if (img.complete) {
					callback(img.width, img.height);
				} else {
					img.onload = function () {
						callback(img.width, img.height);
					}
				}

			},
			formatfile:function (type){
					if(type=='jpg'||type=='jpeg'||type=='png'||type=='gif'||type=='psd'){
						return 1;
					}else if(type=='mp4'||type=='avi'||type=='wmv'||type=='mpg'||type=='psd'||type=='swf'||type=='mpeg'){
						return 2;
					}else{
						return 0;
					}
			}

		}
	};

});

